﻿using Atomic;
using JTActiveSafety.Gateway.Abstractions;
using JTActiveSafety.Gateway.Configurations;
using JTActiveSafety.Gateway.Impl;
using JTActiveSafety.Gateway.Internal;
using JTActiveSafety.Gateway.Jobs;
using JTActiveSafety.Gateway.Services;
using JTActiveSafety.Gateway.Sessions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("JTActiveSafety.Gateway.Test")]

namespace JTActiveSafety.Gateway
{
    public static class JTActiveSafetyGatewayExtensions
    {

        public static IJTActiveSafetyGatewayBuilder AddJTActiveSafetyGateway(this IServiceCollection  serviceDescriptors, IConfiguration configuration)
        {
            IJTActiveSafetyBuilder builder = new JTActiveSafetyBuilderDefault(serviceDescriptors);
            builder.Services.Configure<JTActiveSafetyConfiguration>(configuration.GetSection("JTActiveSafetyConfiguration"));
            IJTActiveSafetyGatewayBuilder jT1078GatewayBuilderDefault = new JTActiveSafetyGatewayBuilderDefault(builder);
            jT1078GatewayBuilderDefault.AddJTActiveSafetyCore();
            return jT1078GatewayBuilderDefault;
        }

        public static IJTActiveSafetyGatewayBuilder AddJTActiveSafetyGateway(this IServiceCollection serviceDescriptors, Action<JTActiveSafetyConfiguration> jt1078Options)
        {
            IJTActiveSafetyBuilder builder = new JTActiveSafetyBuilderDefault(serviceDescriptors);
            builder.Services.Configure(jt1078Options);
            IJTActiveSafetyGatewayBuilder jT1078GatewayBuilderDefault = new JTActiveSafetyGatewayBuilderDefault(builder);
            jT1078GatewayBuilderDefault.AddJTActiveSafetyCore();
            return jT1078GatewayBuilderDefault;
        }

        public static IJTActiveSafetyGatewayBuilder AddTcp(this IJTActiveSafetyGatewayBuilder builder)
        {
            builder.JTActiveSafetyBuilder.Services.AddSingleton<IConnectionListenerFactory, SocketTransportFactory>();
            builder.JTActiveSafetyBuilder.Services.AddSingleton<IServer, KestrelServerImpl>();
            builder.JTActiveSafetyBuilder.Services.AddSingleton<SocketTransportOptions>();
            //builder.JTActiveSafetyBuilder.Services.AddHostedService<JTActiveSafetyTcpReceiveTimeoutJob>();
            builder.JTActiveSafetyBuilder.Services.AddHostedService<JTActiveSafetyTcpServer>();

            //builder.JTActiveSafetyBuilder.Services.TryAddSingleton<IConnectionListenerFactory, SocketTransportFactory>();
            //builder.JTActiveSafetyBuilder.Services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();
            //builder.JTActiveSafetyBuilder.Services.AddSingleton<IServer, KestrelServerImpl>();

            return builder;
        }



        //public static IJTActiveSafetyGatewayBuilder AddUdp(this IJTActiveSafetyGatewayBuilder builder)
        //{
        //    builder.JTActiveSafetyBuilder.Services.AddHostedService<JTActiveSafetyUdpReceiveTimeoutJob>();
        //    builder.JTActiveSafetyBuilder.Services.AddHostedService<JTActiveSafetyUdpServer>();
        //    return builder;
        //}

        //public static IJTActiveSafetyGatewayBuilder AddHttp(this IJTActiveSafetyGatewayBuilder builder)
        //{
        //    builder.JTActiveSafetyBuilder.Services.AddSingleton<IJTActiveSafetyAuthorization, JTActiveSafetyAuthorizationDefault>();
        //    builder.JTActiveSafetyBuilder.Services.AddSingleton<JTActiveSafetyHttpSessionManager>();
        //    builder.JTActiveSafetyBuilder.Services.AddHostedService<JTActiveSafetyHttpServer>();
        //    return builder;
        //}

        //public static IJTActiveSafetyGatewayBuilder AddHttp<TIJTActiveSafetyAuthorization>(this IJTActiveSafetyGatewayBuilder builder)
        //    where TIJTActiveSafetyAuthorization: IJTActiveSafetyAuthorization
        //{
        //    builder.JTActiveSafetyBuilder.Services.AddSingleton(typeof(IJTActiveSafetyAuthorization), typeof(TIJTActiveSafetyAuthorization));
        //    builder.JTActiveSafetyBuilder.Services.AddSingleton<JTActiveSafetyHttpSessionManager>();
        //    builder.JTActiveSafetyBuilder.Services.AddHostedService<JTActiveSafetyHttpServer>();
        //    return builder;
        //}
        
        //public static IJTActiveSafetyGatewayBuilder AddCoordinatorHttpClient(this IJTActiveSafetyGatewayBuilder builder)
        //{
        //    builder.JTActiveSafetyBuilder.Services.AddSingleton<JTActiveSafetyCoordinatorHttpClient>();
        //    builder.JTActiveSafetyBuilder.Services.AddHostedService<JTActiveSafetyHeartbeatJob>();
        //    return builder;
        //}

        internal static IJTActiveSafetyGatewayBuilder AddJTActiveSafetyCore(this IJTActiveSafetyGatewayBuilder builder)
        {
            //builder.JTActiveSafetyBuilder.Services.AddSingleton<JTActiveSafetySessionNoticeService>();
            builder.JTActiveSafetyBuilder.Services.AddSingleton<JTActiveSafetySessionManager>();
            builder.JTActiveSafetyBuilder.Services.AddSingleton<ConnectionManager>();
            //builder.JTActiveSafetyBuilder.Services.AddHostedService<JTActiveSafetySessionNoticeJob>();
            return builder;
        }
 
    }
}